#!/bin/sh
################################################################################
# conf_parallel
#
# This script retrieves and processes the MiniMyth configure file.
################################################################################
. /etc/rc.d/functions

conf_variable_write() {
    /bin/echo -n ${2} >> ${1}
    /bin/echo -n "="  >> ${1}
    /bin/echo -n "'"  >> ${1}
    /bin/echo -n ${3} >> ${1}
    /bin/echo    "'"  >> ${1}

    return 0
}

start() {

    local file
    local file_list
    local variable

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_parallel] Starting..."

    # Read MiniMyth configuration file
    . /etc/minimyth.d/minimyth.conf

    file_list=`cd /etc/rc.d/init.d/conf.d ; /bin/ls -1 P_*`

    # Read MiniMyth configuration processing files
    for file in ${file_list} ; do
        . /etc/rc.d/init.d/conf.d/${file}
    done

    # Write processed MiniMyth configuration file
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_parallel] processing configuration: writing variables..."
    /bin/rm -f '/etc/conf.d/minimyth~'
    /bin/touch '/etc/conf.d/minimyth~'
    /bin/chmod 644 '/etc/conf.d/minimyth~'
    for file in ${file_list} ; do
        conf_write_${file} '/etc/conf.d/minimyth~'
        # /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_parallel] conf_write_${file} done..."
    done
    /bin/rm -f '/etc/conf.d/minimyth~~'
    /bin/touch '/etc/conf.d/minimyth~~'
    /bin/chmod 644 '/etc/conf.d/minimyth~~'
    /bin/cat '/etc/conf.d/minimyth' >> '/etc/conf.d/minimyth~'
    /usr/bin/sort '/etc/conf.d/minimyth~' > '/etc/conf.d/minimyth~~'
    /bin/mv -f '/etc/conf.d/minimyth~~' '/etc/conf.d/minimyth~'
    /bin/mv -f '/etc/conf.d/minimyth~' '/etc/conf.d/minimyth'

    # If there are any errors, then do not continue
    if [ -e /var/log/minimyth.err.log ] && [ `/bin/cat /var/log/minimyth.err.log | /usr/bin/wc -l` -gt 0 ] ; then
        mm_message_output err "error: check '/var/log/minimyth.err.log' for details."
        exit 1
    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/conf_parallel] Started with return code=0"

    return 0
}

stop() {
    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
